package com.ChenYaRou.www.utils;

import com.ChenYaRou.www.constant.JsonBeanConstant;
import com.ChenYaRou.www.dao.*;
import com.ChenYaRou.www.dao.impl.*;
import com.ChenYaRou.www.po.*;

import java.sql.SQLException;
import java.util.Collections;
import java.util.List;

/**
 * @ClassName UpdateBeanServletController
 * @Description TODO
 * @Author 86183
 * @Date 2022/5/2 0:56
 * @Version 1.0
 **/
public class UpdateOrInsertOrSelectMoreAllBean {
    private volatile static UpdateOrInsertOrSelectMoreAllBean updateOrInsertAllBean;
    private UpdateOrInsertOrSelectMoreAllBean(){}
    public static UpdateOrInsertOrSelectMoreAllBean getInstance(){
        if(updateOrInsertAllBean==null){
            synchronized (UpdateOrInsertOrSelectMoreAllBean.class){
                if(updateOrInsertAllBean==null){
                    updateOrInsertAllBean=new UpdateOrInsertOrSelectMoreAllBean();
                }
            }
        }
        return updateOrInsertAllBean;
    }

    /**
     * 更新各模块的内功能
     * @param newObject
     * @param oldObject
     * @param <K>
     * @return
     */
    public  <K> String update(K newObject,K oldObject) {
        Class<?> objectClass = newObject.getClass();
        int update=0;
        try{
            if(objectClass.equals(DetailedUser.class) ){
                DetailedUserDao detailedUserDao=new DetailedUserDaoImpl();
                update =detailedUserDao.update((DetailedUser) newObject, (DetailedUser) oldObject);
            }else if(objectClass.equals(Collection.class)){
                CollectionDao collectionDao=new CollectionDaoImpl();
                update = collectionDao.update((Collection) newObject, (Collection) oldObject);
            }else if(objectClass.equals(Orders.class)){
                OrdersDao ordersDao=new OrdersDaoImpl();
                update = ordersDao.update((Orders) newObject, (Orders) oldObject);
            }else if(objectClass.equals(Car.class)){
                CarDao carDao=new CarDaoImpl();
                update = carDao.update((Car) newObject,(Car) oldObject);
            }else if(objectClass.equals(Message.class)){
                MessageDao messageDao=new MessageDaoImpl();
                update = messageDao.update((Message) newObject, (Message) oldObject);
            }else if(objectClass.equals(Shop.class)){
                ShopDao shopDao=new ShopDaoImpl();
                update = shopDao.update((Shop) newObject, (Shop) oldObject);
            }else if(objectClass.equals(Food.class)){
                FoodDao foodDao=new FoodDapImpl();
                update = foodDao.update((Food) newObject, (Food) oldObject);
            }else if(objectClass.equals(TicketShop.class)){
                TicketShopDao ticketShopDao=new TicketShopDaoImpl();
                update = ticketShopDao.update((TicketShop) newObject, (TicketShop) oldObject);
            }
            if(update>0){
                return JsonBean.generateJson(JsonBeanConstant.GET,JsonBeanConstant.SUCCESS,null);
            }
        } catch (SQLException | IllegalAccessException  e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 对所有insert功能的包装
     * @param object
     * @param <K>
     * @return
     */
    public <K> String insert(K object){
        if(object!=null){
            Class<?> objectClass = object.getClass();
            int insert=0;
            try{
                if(objectClass.equals(DetailedUser.class) ){
                    DetailedUserDao detailedUserDao=new DetailedUserDaoImpl();
                    insert = detailedUserDao.insert((DetailedUser) object);
                }else if(objectClass.equals(Collection.class)){
                    CollectionDao collectionDao=new CollectionDaoImpl();
                    insert = collectionDao.insert((Collection) object);
                }else if(objectClass.equals(Orders.class)){
                    OrdersDao ordersDao=new OrdersDaoImpl();
                    insert = ordersDao.insert((Orders) object);
                }else if(objectClass.equals(Car.class)){
                    CarDao carDao=new CarDaoImpl();
                    insert = carDao.insert((Car) object);
                }else if(objectClass.equals(Message.class)){
                    MessageDao messageDao=new MessageDaoImpl();
                    insert = messageDao.insert((Message) object);
                }else if(objectClass.equals(Notice.class)){
                    NoticeDao noticeDao=new NoticeDaoImpl();
                    insert = noticeDao.insert((Notice) object);
                }else if(objectClass.equals(User.class)){
                    UserDao userDao=new UserDaoImpl();
                    insert = userDao.insert((User) object);
                }else if(objectClass.equals(Food.class)){
                    FoodDao foodDao=new FoodDapImpl();
                    insert = foodDao.insert((Food) object);
                }else if(objectClass.equals(TicketShop.class)){
                    TicketShopDao ticketShopDao=new TicketShopDaoImpl();
                    insert = ticketShopDao.insert((TicketShop) object);
                }
                if(insert>0){
                    return JsonBean.generateJson(JsonBeanConstant.GET,JsonBeanConstant.SUCCESS,null);
                }
            } catch (SQLException | IllegalAccessException  e) {
                e.printStackTrace();
            }
            return JsonBean.operateList(JsonBeanConstant.FAIL_UPDATE,null);
        }
        return JsonBean.operateList(JsonBeanConstant.FRONT_ERROR,null);
    }

    /**
     * 获取集合的功能包装
     * @param object
     * @param <K>
     * @return
     */
    @SuppressWarnings("unchecked")
    public <K> List<K>  selectMore(K object){
        if(object!=null){
            List<K> objectList=null;
            Class<?> objectClass = object.getClass();
                try {
                    if(objectClass.equals(Message.class)){
                        MessageDao messageDao=new MessageDaoImpl();
                        objectList = (List<K>) messageDao.selectMore((Message) object);
                    }else if(objectClass.equals(Collection.class)){
                        CollectionDao collectionDao=new CollectionDaoImpl();
                        objectList = (List<K>)collectionDao.selectMore((Collection) object);
                    }else if(objectClass.equals(Orders.class)){
                        OrdersDao ordersDao=new OrdersDaoImpl();
                        objectList = (List<K>) ordersDao.selectMore((Orders) object);
                    }else if(objectClass.equals(Ticket.class)){
                        TicketDao ticketDao=new TicketDaoImpl();
                        objectList = (List<K>) ticketDao.select((Ticket) object);
                    }else if(objectClass.equals(Car.class)){
                        CarDao carDao=new CarDaoImpl();
                        objectList = (List<K>) carDao.selectMore((Car) object);
                    }else if(objectClass.equals(Shop.class)){
                        ShopDao shopDao=new ShopDaoImpl();
                        objectList = (List<K>) shopDao.selectMore((Shop) object);
                    }else if(objectClass.equals(Notice.class)){
                        NoticeDao noticeDao=new NoticeDaoImpl();
                        objectList = (List<K>) noticeDao.select((Notice) object);
                        //在数据库中已经是新到旧的时间安排 为何取出时还是旧到新?
                        Collections.reverse(objectList);
                    }else if(objectClass.equals(District.class)){
                        DistrictDao districtDao=new DistrictDaoImpl();
                        objectList = (List<K>) districtDao.select((District) object);
                    }else if(objectClass.equals(Street.class)){
                        StreetDao streetDao=new StreetDaoImpl();
                        objectList = (List<K>) streetDao.select((Street) object);
                    }else if(objectClass.equals(Food.class)){
                        FoodDao foodDao=new FoodDapImpl();
                        objectList = (List<K>) foodDao.selectMore((Food) object);
                    }else if(objectClass.equals(TicketShop.class)){
                        TicketShopDao ticketShopDao=new TicketShopDaoImpl();
                        objectList = (List<K>) ticketShopDao.select((TicketShop) object);
                    }
                    return objectList;
                } catch (IllegalAccessException | SQLException | NoSuchFieldException |
                        InstantiationException e) {
                    e.printStackTrace();
            }
        }
        return null;
    }

    /**
     * 多条sql语句操作后,返回不带data的json
     * @param sql
     * @return
     */
    public String insertOrUpdateMore(String... sql)  {
        int i = JdbcUtil.getInstance().insertOrUpdateMore(sql);
        if(i>0){
            return JsonBean.generateJson(JsonBeanConstant.GET,JsonBeanConstant.SUCCESS,null);
        }
        return JsonBean.operateList(JsonBeanConstant.FAIL_UPDATE,null);
    }
}
